/*
 * Android Runner is a multiplayer GPS game fully written by Xurxo Mendez Perez
 * 
 * Copyright (C) 2009 Xurxo Mendez Perez
 *   
 * This file is part of Android Runner.
 * 
 * Android Runner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Android Runner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Android Runner.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.sonxurxo.gpsgame.client.cv.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import es.sonxurxo.gpsgame.client.R;
import es.sonxurxo.gpsgame.client.cv.common.actions.CommonActions;
import es.sonxurxo.gpsgame.client.cv.common.dialogs.CommonDialogs;
import es.sonxurxo.gpsgame.client.cv.util.constants.CustomResultCodes;
import es.sonxurxo.gpsgame.client.model.service.to.Game;
import es.sonxurxo.gpsgame.client.model.service.to.Player;
import es.sonxurxo.gpsgame.client.util.DateOperations;

/**
 * 
 * @author "Xurxo Mendez Perez"
 *
 */
public class ViewGameActivity extends Activity implements Abandonable {

	private TextView gameIdView, gameCityView, gameStartDateView, 
	gameFinishDateView, gamePointsToWinView, gameAvailablePointsView, 
	gameCurrentPlayersView, gameMaxPlayersView;
	
	private Button removeButton;
	
	private Player player;
	private Game game;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.view_game_page);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);
        
		Bundle extras = getIntent().getExtras();
		player = (Player)extras.getSerializable("player");
		((TextView)findViewById(R.id.title_right_text)).setText("Game Info");
		game = (Game)extras.getSerializable("game");
		
		gameIdView = (TextView)findViewById(R.id.vg1_tv_gameid);
		gameIdView.setText(String.valueOf(game.getGameId()));

		gameCityView = (TextView)findViewById(R.id.vg1_tv_gamecity);
	    gameCityView.setText(game.getCity());
	    
	    gameStartDateView = (TextView)findViewById(R.id.vg1_tv_gamestartdate);
	    gameStartDateView.setText(DateOperations.formatDate(game.getStartDate()));
	    
	    gameFinishDateView = (TextView)findViewById(R.id.vg1_tv_gamefinishdate);
	    gameFinishDateView.setText(DateOperations.formatDate(game.getFinishDate()));
	    
	    gamePointsToWinView = (TextView)findViewById(R.id.vg1_tv_gamepointstowin);
	    gamePointsToWinView.setText(String.valueOf(game.getPointsToWin()));
	    
	    gameAvailablePointsView = (TextView)findViewById(R.id.vg1_tv_availablepoints);
	    gameAvailablePointsView.setText(String.valueOf(game.getAvailablePoints()));
	    
	    gameCurrentPlayersView = (TextView)findViewById(R.id.vg1_tv_gamecurrentplayers);
	    gameCurrentPlayersView.setText(String.valueOf(game.getCurrentPlayers()));
	    
	    gameMaxPlayersView = (TextView)findViewById(R.id.vg1_tv_gamemaxplayers);
	    gameMaxPlayersView.setText(" / " + String.valueOf(game.getMaxPlayers()));
	    if (game.getCurrentPlayers() 
    			== game.getMaxPlayers()) {
    		gameMaxPlayersView.append(" (COMPLETE)");
    	}
	    removeButton = (Button)findViewById(R.id.vg1_b_removeFromGame);
	    removeButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				doAbandonGame();
			}
	    	
	    });
	}
	
	private void doAbandonGame() {
		CommonActions.launchAbandonGameThread(
					player, game, this);
	}

	@Override
    protected Dialog onCreateDialog(int id) {
		return CommonDialogs.createDialog(id, this);
	}

	public void doAfterAbandon() {
		Intent mIntent = new Intent();
		Bundle extras = new Bundle();
		player.setLocation(null);
		extras.putSerializable("player", player);
		mIntent.putExtras(extras);
		setResult(CustomResultCodes.ABANDONED_RESULT_CODE, mIntent);
		finish();
	}
}
